package com.itheima.goods.controller;


import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
@CrossOrigin
public class SpecController {

    @Autowired
    private SpecService specService;


    @GetMapping("/findAll")
    public Result findAll(){

        try {
            List<Spec> specList = specService.findAll();

            if (ObjectUtils.isEmpty(specList)){

                return new Result(true, StatusCode.OK,"查询结果为空");
            }

            return new Result(true,StatusCode.OK,"查询成功",specList);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")Integer page,@PathVariable("size")Integer size){

        try {
            PageResult pageResult = specService.findPage(page, size);

            if (ObjectUtils.isEmpty(pageResult)){

                return new Result(true, StatusCode.OK,"查询结果为空");
            }

            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        } catch (Exception e) {

            e.printStackTrace();

            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }


    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){

        try {
            Spec spec = specService.findById(id);

            if (ObjectUtils.isEmpty(spec)){

                return new Result(true,StatusCode.OK,"查询结果为空");
            }

            return new Result(true,StatusCode.OK,"查询成功",spec);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,StatusCode.ERROR,"查询失败");
        }


    }


    @PostMapping("/add")
    public Result add(@RequestBody Spec spec){

        try {
            Boolean result = specService.add(spec);

            if (result){

                return new Result(true,StatusCode.OK,"添加成功");
            }

            return new Result(false,StatusCode.ERROR,"添加失败");
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,StatusCode.ERROR,"添加失败");
        }

    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id")Integer id,@RequestBody Spec spec){

        try {
            Boolean result = specService.update(id,spec);

            if (result){

                return new Result(true,StatusCode.OK,"修改成功");
            }

            return new Result(false,StatusCode.ERROR,"修改失败");
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,StatusCode.ERROR,"修改失败");
        }

    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")Integer id){


        try {
            Boolean result = specService.delete(id);

            if (result){

                return new Result(true,StatusCode.OK,"删除成功");
            }

            return new Result(false,StatusCode.ERROR,"删除失败");
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,StatusCode.ERROR,"删除失败");
        }

    }
}
